<?php
/**
 * Groups module for XOOPS
 *
 * You may not change or alter any portion of this comment or credits
 * of supporting developers from this source code or any supporting source code 
 * which is considered copyrighted (c) material of the original comment or credit authors.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *
 * @copyright       The Groups Project http://code.google.com/p/groupsforxoops/
 * @license         http://www.fsf.org/copyleft/gpl.html GNU public license
 * @package         groups
 * @since           2.3.0
 * @author          Hui Xiao < xh.8326@gmail.com >
 * @version         $Id: threads.php Sat Jul 18 13:11:34 CST 2009
 */

if ( !defined("XOOPS_ROOT_PATH") )  exit;
if ( !class_exists("xoGroups") ) {
	include dirname(dirname(__FILE__))."/include/groups.php";
}
class groupsThreads extends XoopsObject 
{
	public function __construct() {
		$this->initVar("tid", XOBJ_DTYPE_INT, null, false); // id
		$this->initVar("group_id", XOBJ_DTYPE_INT,0,true); // 所属群组ID
		$this->initVar("column_id", XOBJ_DTYPE_INT,0,true); // 分栏ID
		$this->initVar("uid", XOBJ_DTYPE_INT,0,true); // 作者ID
		$this->initVar("threads_author", XOBJ_DTYPE_TXTBOX,null,true); // 作者
		$this->initVar("threads_subject", XOBJ_DTYPE_TXTBOX,null,true,120); // 标题
		$this->initVar("threads_create_time", XOBJ_DTYPE_INT,0,false); // 发表时间
		$this->initVar("threads_update_time", XOBJ_DTYPE_INT,0,false); // 最后一个回复的发表时间
		$this->initVar("threads_last_poster", XOBJ_DTYPE_TXTBOX,null,true); // 最后一个回复的作者
		$this->initVar("threads_last_uid", XOBJ_DTYPE_INT,0,true); // 最后一个回复的uid
		$this->initVar("threads_icon", XOBJ_DTYPE_TXTBOX,null,true); // 主题的ICON	
		$this->initVar("threads_views", XOBJ_DTYPE_INT,0,false); // 被浏览的次数
		$this->initVar("threads_replies", XOBJ_DTYPE_INT,0,false); // 被回复的次数
		$this->initVar("threads_displaytop", XOBJ_DTYPE_INT,0,false); // 主题被置顶
		$this->initVar("threads_highlight", XOBJ_DTYPE_TXTBOX,0,false); // 高亮显示
		$this->initVar("threads_digest", XOBJ_DTYPE_INT,0,false); // 精华
		$this->initVar("threads_status", XOBJ_DTYPE_INT,0,false); // 主题的状态 0 正常 1被锁 2关闭
		$this->initVar("threads_attachments", XOBJ_DTYPE_INT,0,false); // 是否存在附件 0 没有 1有
	}	
	
	public function createTime($format="L") {
	    return formatTimestamp($this->getVar("threads_create_time"),$format);
	}
	
	public function updateTime($format="L") {
	    return formatTimestamp($this->getVar("threads_update_time"),$format);
	}

    public function icon() {
	    return $this->getVar("threads_icon") ? $this->getVar("threads_icon","n") : false;
	}
}

class GroupsThreadsHandler extends XoopsPersistableObjectHandler
{
	public function __construct($db) {
        parent::__construct($db,"groups_threads","GroupsThreads","tid");
    }

    public function setThread( array $params ) {
        $myts = MyTextSanitizer::getInstance();
        if ( empty($params) || !is_array($params) ) return false;
    	$group_id = isset($params["group_id"]) ? intval($params["group_id"]) : 0 ;
        if ( empty($group_id) ) return false;

        $tid = isset($params["tid"]) ? intval($params["tid"]) : 0 ;
        $time = time();
        if ( !empty($tid) ) {
            $obj = $this->get($tid);
        } else {
            $obj = $this->get();
            $obj->setVar("threads_create_time",$time);
        }
        $obj->setVar("threads_update_time",$time);
    	foreach ( $params as $key => $val ) {
			if ( !isset($obj->vars[$key]) || $myts->stripSlashesGPC($val) == $obj->getVar($key) ) continue;
			$obj->setVar($key, $val);
		}
    	if ( $tid = $this->insert($obj) ) {
    	    return $tid;
    	}
    	return false;
    }

    /**
     * select threads 
     *
     * @param array $params
     * @param bool $asObj
     * @return array $ret array / object
     */
    public function getThreads( array $params, $asObj=true ) {
        $criteria = xoGroups::getCriteria($params);
        $fields = ( isset($params["fields"]) && is_array($params["fields"]) ) ? $params["fields"] : null ;
        $ret = $this->getAll($criteria, $fields, $asObj);
        return $ret;
    }
    
    /**
     * for page nav
     *
     * @param array $params
     * @return string $pagenav
     */
    public function getPageNav( array $params ) {
        $pagenav = "";
        $criteria = xoGroups::getCriteria($params);
        $counts = $this->getCount($criteria);
        $extra = isset($params["extra"]) ? trim($params["extra"]) : "";
        if( $counts > $params["limit"]){	
        	$nav = new XoopsPageNav($counts, $params["limit"], $params["start"], "start",$extra);
        	$pagenav = $nav->renderNav();
        }
        return $pagenav;
    }
    
}